
require 'active_record/fixtures'

class AddSystemData < ActiveRecord::Migration
  def self.up
    create_new_user_role_rights()
    create_root_role_rights_project_member()
  end

  def self.down
  end

  private

  def self.create_new_user_role_rights
    user_right_file_path = File.join(File.dirname(__FILE__), "data","/new_user_rights.yml")
    user_rights = YAML::load( open( user_right_file_path ))
    new_user_role = Role.new
    new_user_role.name = "user"
    for right_info in user_rights
      right = new_user_role.rights.build( right_info[1] )
    end
    new_user_role.save
  end
  def self.create_root_role_rights_project_member

    Fixtures.create_fixtures(File.join(File.dirname(__FILE__), "data"), "projects")

    root_user = User.new
    root_user.id = 1
    root_user.login = "root"
    root_user.password = root_user.password_confirmation = "root"
    root_user.email = "root@127.0.0.1"
    root_user.save
    
    root_right_file_path = File.join(File.dirname(__FILE__), "data","/root_rights.yml")
    root_rights = YAML::load(open( root_right_file_path  ))
    root_role = Role.new
    root_role.name = "Root"
    for right_info in root_rights
      right = root_role.rights.build( right_info[1] )
    end
    root_role.save

    system_member = ProjectMember.new
    system_member.project = Project.find_by_title("system")
    system_member.user = root_user
    system_member.role = root_role
    system_member.save
  end
end
